﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PubSubLite.Messaging.MessageChannels
{
    /// <summary>
    /// A simple implementation of a <see cref="IMessageChannel"/>.
    /// It justs forwards the messages as events within the current thread.
    /// </summary>
    public class InMemoryMessageChannel : MessageChannel, IMessageChannel
    {
        /// <summary>
        /// A singleton instance of a MessageChannel.
        /// </summary>

        void IMessageChannel.Send(IMessageBase message)
        {
            OnMessageReceived(message);
        }

        private event EventHandler<MessageReceivedEventArgs> _messageReceived;

        event EventHandler<MessageReceivedEventArgs> IMessageChannel.MessageReceived
        {
            add { _messageReceived += value; }
            remove { _messageReceived -= value; }
        }

        private void OnMessageReceived(IMessageBase message)
        {
            if (_messageReceived != null)
            {
                _messageReceived(this, new MessageReceivedEventArgs(message));
            }
        }
    }
}
